package com.powernode.rules.service.impl;

import com.alibaba.fastjson.JSON;
import com.powernode.model.form.rules.RewardRuleRequest;
import com.powernode.model.form.rules.RewardRuleRequestForm;
import com.powernode.model.vo.rules.RewardRuleResponse;
import com.powernode.model.vo.rules.RewardRuleResponseVo;
import com.powernode.rules.config.DroolsHelper;
import com.powernode.rules.service.RewardRuleService;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class RewardRuleServiceImpl implements RewardRuleService {
    @Override
    public RewardRuleResponseVo calculateOrderRewardFee(RewardRuleRequestForm rewardRuleRequestForm) {
        // 封装传入对象
        RewardRuleRequest rewardRuleRequest = new RewardRuleRequest();
        rewardRuleRequest.setOrderNum(rewardRuleRequestForm.getOrderNum());
        log.info("传入参数：{}", JSON.toJSONString(rewardRuleRequest));
        KieSession kieSession = DroolsHelper.loadForRule("rules/RewardRule.drl");
        // 封装返回对象
        RewardRuleResponse rewardRuleResponse = new RewardRuleResponse();
        kieSession.setGlobal("rewardRuleResponse", rewardRuleResponse);
        // 设置订单对象
        kieSession.insert(rewardRuleRequest);
        // 触发规则
        kieSession.fireAllRules();
        // 中止会话
        kieSession.dispose();
        log.info("计算结果：{}", JSON.toJSONString(rewardRuleResponse));
        // 封装返回对象
        RewardRuleResponseVo rewardRuleResponseVo = new RewardRuleResponseVo();
        // 没有查询数据库这里写死
        rewardRuleResponseVo.setRewardRuleId(1L);
        rewardRuleResponseVo.setRewardAmount(rewardRuleResponse.getRewardAmount());
        return rewardRuleResponseVo;
    }
}